﻿const spawn = require('child_process').spawn;
const {
    task_index,
    code_path
} = require('./../keyword');

/**
 * @param year 年
 * @param eday 第几个八天
 * */
const runDraught = (year,eday,statement) => {
    let logName = new Date().getTime();
    let taskContent = `start cmd /k 
        "cd ${code_path} &
        mkdir Z:\\draught_index_calculation_module\\tmp\\${logName} &
        gdal_py.exe  
		runShell.py 
		${year} ${eday} Z:\\draught_index_calculation_module\\tmp\\${logName} > Z:\\draught_index_calculation_module/log/${logName} & 
		ren Z:\\draught_index_calculation_module\\log\\${logName} ${logName}.ok & exit"`.split('\n').map(_ => _.trim()).join(' ');
    let runner = spawn(taskContent,{
        shell: true
    });

    return {
        log: 'Z:\\draught_index_calculation_module/log/' + logName,
        start_time: logName,
        end_time: '',
        task_content: taskContent,
        statement,
        schedule: 0,
        task_index,
        other: JSON.stringify({
            dirName: logName,
            year,
            eday
        })
    }
};

// 注意这里只能是 function 而不能是 λ 表达式
// module.exports = () => {} 将无法运行
module.exports = function(db,R) {
    let newDT = runDraught(
        R.content.param.year,
        R.content.param.eday,
        R.content.param.statement);
    this.defaultAction.bind(this,R,'save',{
        data: newDT
    })();
};